<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!-- public quote -->
<%@include file="/jsp/comm_css_include.jsp"%>
<!-- jqgrid-->
<link
	href="${basePath}/css/plugins/jqgriddemo/css/ui.jqgrid-bootstrap.min.css"
	rel="stylesheet" />
<!doctype html>
<html>
<head>
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="iboxtitle">
			<span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;工具箱>业务员变更</span>
		</div>
		<div class="ibox-content">
			<div role="form" class="form-inline">
				<div class="form-group">
					<label class="sr-only">客户名：</label> <input type="text"
						placeholder="客户名" id="customerName" name="customerName"
						class="form-control">
				</div>
				<div class="form-group">
					<label class="sr-only">身份证号：</label> <input type="text"
						placeholder="身份证号" id="customerNo" name="customerNo"
						class="form-control">
				</div>
				<input type="hidden" id="salesmanId" />
				<button class="zhixcx" type="button" onclick="query();">执行查询</button>
			</div>
		</div>
		<div class="ibox-content">
			<div class="jqGrid_wrapper">
				<table id="table_list_1"></table>
				<div id="pager_list_1"></div>
			</div>
		</div>
	</div>

	<form class="form-horizontal m-t" id="commentForm">
		<div class="ibox-content" id="hiddenstyle" style="display: none;">
			<div class="form-group">
				<label class="col-sm-3 control-label">原业务员：</label>
				<div class="col-sm-8">
					<input class="form-control" placeholder="原业务员" type="text"
						id="salemanNamea" name="salemanName" readonly />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">新业务员：</label>
				<div class="col-sm-8">
					<input type="hidden" id="id" name="id" /> <input type="hidden"
						id="salesmanId" name="salesmanId" /> <input type="hidden"
						id="cusId" name="cusId" /> <input
						class="form-control smalliconcur smallicon" placeholder="业务员(必填)"
						type="text" id="salesmanName" name="salesmanName"
						onclick="salemancho('',false);" readonly required="required" />
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-3">
					<button class="btn dpbtn_blue" type="button" onclick="change();">替换</button>
				</div>
			</div>
		</div>
		<div class="content-show" id="searchDiv" style="display: none;">
			<div class="searchtop">
				<input type="text" class="form-control smallicon  fixedsearch"
					id="searchname" name="salemanName" placeholder="业务员姓名"
					onkeyup="salemancho(this.value, true);"
					onafterpaste="salemancho(this.value, true);">
			</div>
			<ul class="cho-ul" id="searchUl"></ul>
		</div>
	</form>
	<!-- public quote -->
	<%@include file="/jsp/comm_js_include.jsp"%>

	<!-- jqGrid -->
	<script type="text/javascript" charset="utf-8"
		src="${basePath}/js/plugins/jqgriddemo/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" charset="utf-8"
		src="${basePath}/js/plugins/jqgriddemo/js/grid.locale-cn.min.js"></script>
	<script type="text/javascript"
		src="${basePath}/js/zTreeStyle/jquery.ztree.core.js" charset="utf-8"></script>
	<script type="text/javascript"
		src="${basePath}/js/zTreeStyle/jquery.ztree.excheck.js"
		charset="utf-8"></script>
	<!-- custom js -->
	<%@include
		file="/js/common-dpqc/dpqc-validate/comm_validate_include.jsp"%>
	<!-- 业务员-->
	<!--
    	作者：zhangpp
    	时间：2017-04-06
    	描述：js
    -->
	<script type="text/javascript">
	    
	    var winWidth = $(window).width();
		var winHeight = $(window).height();
		var index;
	    jQuery(function($) {  
	    	getData();
	    });
	    
	    var getData = function()
	    {
	    	var winWidth = $('div.ibox-content').width();
	       var winHeight = document.body.offsetHeight - 300;
	       var win = winWidth / 7 - 20;
  	 	   $.jgrid.defaults.styleUI = 'Bootstrap';
  	 	   
		  jQuery("#table_list_1").jqGrid
		  ({
			url:'${basePath}/YwBusinessController/multiList',
	        datatype : "json",
	        colNames : [ '主键', '客户姓名', '身份证号码','签约贷款额', '经销商','经销商等级', '部门','业务员',  '银行','业务编码','客户id','业务员id'],
	        colModel : [ 
	                     {name: 'replaceId', index: 'id', hidden:true},
	            		 {name: 'customerName',index: 'customerName',width: 130,align: "center"},
	            		 {name: 'customerNo',index: 'customerNo',width: 170,align: "center"},
	            		 {name: 'signLoanAmount',index: 'signLoanAmount',width: 130,align: "center"},
	               		 {name: 'dealerName',index: 'dealerName',width: 300,align: "center"},
	               		 {name: 'dealerLevel',index: 'dealerLevel',width: 130,align: "center"},
	            		 {name: 'orgName',index: 'orgName',width: 130,align: "center"},
	            		 {name: 'salesmanName',index: 'salesmanName',width: 130,align: "center"},
	            		 {name: 'bankLoanDisplay',index: 'bankLoan',width: 300,align: "center"},
	            		 {name: 'busiCode', index: 'busiCode', hidden:true},
	            		 {name: 'customerId', index: 'customerId', hidden:true},
	            		 {name: 'salesmanId',index: 'salesmanId', hidden:true}
	                   ],
              mtype : "post",
              height: winHeight,
              width:winWidth,  
              autowidth: false,
              shrinkToFit: false,
              rowNum: 15,
	           rowList: [15, 30, 45],
              viewrecords: true,
              pager: "#pager_list_1",
              hidegrid: false,
              multiselect : false,
              altRows:true,
              altclass:'someClass',
              ondblClickRow:function(rowId,v,e)
              {
            	  var rowData = $("#table_list_1").jqGrid("getRowData", rowId);
            	  var id=rowData.replaceId;
            	  var name=rowData.salesmanName;
            	  var cusId=rowData.customerId;
            	  var salesmanId=rowData.salesmanId;
            	  document.getElementById('salesmanId').value = salesmanId;
            	  document.getElementById('id').value = id;
            	  document.getElementById('cusId').value = cusId;
            	  document.getElementById('salemanNamea').value = name;
            	  document.getElementById("salesmanName").value=""
          		  index = layer.open({
	      	    	  title:'变更业务员',
	      	    	  type: 1,
	      	    	  area: ['500px', '400px'], //宽高
	      	    	  content: $("#hiddenstyle"),
      	    		});
              },
              jsonReader : {
            	  root:"result.rows",
            	  page: "result.page",
            	  total: "result.total",
            	  records: "result.records",
            	  repeatitems: false,
            	  id: "replaceId"
              },
              loadComplete:function(datas){
            	  if (datas.flag == "100100") {
                      layer.msg("查询成功!", {icon: 6, time: 1000});
                      return;
                  }
            	  if(datas.flag == '100802') {
    				  top.location.href = thisLoginUrl;
    				  return;
    			  }

            	  layer.msg(datas.message, {icon: 2});
              }
	      });
	    }
	    
	    var change=function(){
	    	var data={};
	    	data.salesmanName=$("#salesmanName").val();
		    data.salesmanId=$("#salesmanId").val();
		    data.salesmanCode=$("#salersmanCode").val();
		    data.id=$("#cusId").val();
		    var validstatus = $("#commentForm").valid();
 	    	if(validstatus)
 	    	{
		    	$.dpAjax({
		    		url:'${basePath}/BaseCustomerController/modify',
		    		data:data,
	    			success: function(datas){
	    				if(datas.flag == "100100")
						  {
		    				  layer.msg('执行成功！', {icon: 6});
		    				  layer.close(index);
		    				  refresh();
						  }
						  if(datas.flag == "100200")
		                  {
							  $.fn.showDatas({"datas":datas});
		                	  return;
		                  }
	    			}
		    	});
 	    	}
	    }
	    
	    window.onresize = function()
	    {
	    	$("#table_list_1").setGridWidth($(window).width()-80);
	    }
	    
	    var query = function()
	    {
	    	var postData = $("#table_list_1").jqGrid("getGridParam", "postData");
	    	var data = {};
	    	data.customerName = document.getElementById('customerName').value || '';
	    	data.customerNo = document.getElementById('customerNo').value || '';
	    	$.extend(postData, data);
	    	refresh();
	    }
	    
	    var refresh = function()
	    {
	    	$("#table_list_1").jqGrid("setGridParam", { search: true }).trigger("reloadGrid", [{ page: 1}]);
	    }
	    
	    var exete = true;
	    var yeylayer;
	    function salemancho(name,hasOpen){
	       if(exete){
	       	exete = false;
	       	setTimeout(function(){
	       		exete = true;
	    		}, 500);
	       	searchSaleman(name,hasOpen);
	       } else {
	   	    setTimeout(function(){
	       		if(exete) { //超过500ms还没有触发，则再补充执行最后一次
	       			searchSaleman(name, true);
	       		}
	    		}, 500);
	       }
	     }

	    function searchSaleman(name,hasOpen) {
	   	  var name = name || '';
	   	  var salesmanId=$("#salesmanId").val();
	   	  var data={};
	   	  data.name=name;
	   	  data.row=10;
	   	  data.unSalesmanId=salesmanId;
	       	$.dpAjax({
	       		show:false,
	       		url:'${basePath}/BaseSalersmanController/list',
	       		data:data,
	   			success: function(datas){
	   			 	  if(datas.flag == "100100"){
	   			 		var records = datas.result.rows;
	   			 		$searchUl = $('#searchUl');
	   			 		$searchUl.empty();
	   			 		if(records && records.length > 0) {
	   			 			var content = '';
	   				 		for(var i=0;i< records.length; i++){
	   				 			var record = records[i];
	   				 		content +='<a href="javascript:choice(\''+record.replaceId+'\', \''+record.name+'\');"><li><span class="li-name"><lable>姓名:</lable>' + record.name +'('+ record.orgName +')</span><lable>编号:</lable>' + record.salersmanCode +'</li></a>';  
	   				 		}
	   				 		$searchUl.html(content);
	   			 		}
	   			 		if(!hasOpen) {
	   			 			$("#salemanName").blur();
	   						yeylayer=layer.open({
	   						  title:'业务员选择',
	   						  type: 1,
	   						  area: ['580px', '450px'], //宽高
	   						  content: $('#searchDiv')
	   						}); 
	   			 		}
	   			 		
	   					return;
	   				  }
	   				  if(datas.flag == "100200"){
	   					  alert(JSON.stringify(datas));
	                   	  return;
	                     }
	   				  layer.msg(datas.message, {icon: 2});
	                }
	       	 });
	       }
	   	var choice = function(id, name, orgCode, orgName)
	   	{
	   		document.getElementById("salesmanName").value = name;
	   		document.getElementById("salesmanId").value = id;
	   		layer.close(yeylayer);
	   	}
	   	
	    $(function(){
	  	        $("#commentForm").validate({
	  	            //重写showErrors
	  	            showErrors: function (errorMap, errorList) {
	  	                
	  	                $.each(errorList, function (i, v) {
	  	                    layer.tips(v.message, v.element, { time: 1500 });
	  	                    return false;
	  	                });  
	  	            },
	  	            /* 失去焦点时验证 */
	  	            onfocusout: function(element) { $(element).valid(); },
	  	            /*  键盘抬起不验证 */
	  	            onkeyup:false,
	  	        })
	  	        
		    });
    </script>
</body>
</html>
